package com.example.weather.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.weather.DB.DataBaseBean;
import com.example.weather.R;
import com.google.gson.Gson;
import com.example.weather.Bean.Weatherbean;

import java.util.List;

public class CitymanagerAdapter extends BaseAdapter {
    Context context;
    List<DataBaseBean> mdata;

    public CitymanagerAdapter(Context context, List<DataBaseBean> mdata) {
        this.context = context;
        this.mdata = mdata;
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Object getItem(int position) {
        return mdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_city_manager,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        DataBaseBean bean = mdata.get(position);
        viewHolder.item_city_tv_city.setText(bean.getCity());
        Weatherbean weatherbean = new Gson().fromJson(bean.getContent(), Weatherbean.class);
        //获取今日天气情况
        Weatherbean.ResultsBean.WeatherDataBean weatherDataBean = weatherbean.getResults().get(0).getWeather_data().get(0);
        viewHolder.item_city_tv_condition.setText("天气："+weatherDataBean.getWeather());
        String[] split = weatherDataBean.getDate().split("：");
        String replace = split[1].replace(")", "");
        viewHolder.item_city_tv_temp.setText(replace);
        viewHolder.item_city_wind.setText(weatherDataBean.getWind());
        viewHolder.item_city_temprange.setText(weatherDataBean.getTemperature());
        return convertView;
    }

    class ViewHolder{
        TextView item_city_tv_city,item_city_tv_temp,item_city_tv_condition,item_city_wind,item_city_temprange;
        public ViewHolder(View iview){
            item_city_tv_city=iview.findViewById(R.id.item_city_tv_city);
            item_city_tv_temp=iview.findViewById(R.id.item_city_tv_temp);
            item_city_tv_condition=iview.findViewById(R.id.item_city_tv_condition);
            item_city_wind=iview.findViewById(R.id.item_city_wind);
            item_city_temprange=iview.findViewById(R.id.item_city_temprange);
        }
    }
}
